/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.loan;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.loan.Loan;
import src.domain.loan.Loanclosure;
import src.domain.security.Users;
import src.services.loan.LoanModel;
import src.services.loan.LoanclosureModel;
import src.util.Constanta;
import src.util.Messager;
import src.view.security.LoginBean;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class LoanclosureBean implements Serializable {
    
    private Loanclosure loanclosure;  
    private boolean editMode = false, insertionMode=false;
    private String defaultButton="doNothingLoanclose";
    private boolean showLookup=false;
    private Users users;        
    public LoanclosureBean() {
        ambilUser();
        newData();
    }
    
    public boolean isEditMode() {
        return editMode;
    }
    
    public Loanclosure getLoanclosure() {
        return loanclosure;
    }

    public void setLoanclosure(Loanclosure loanclosure) {
        this.loanclosure = loanclosure;
    }

    public void getLoan(String accntNo) {
        boolean found=false;
        if ("null".equals(accntNo)) accntNo = loanclosure.getLoan().getLoanNo();
        if (!accntNo.equals("") && accntNo!=null ) {
            LoanModel helper = new LoanModel();
            Loan loan = helper.getByAccount(accntNo, Constanta.LOANSTATUS_RUNNING);
                        
            if (loan==null) {
                Messager.showWarning("accntNotFound");
                this.loanclosure.setLoan(new Loan());
                
            } else {
                found=true;
                this.loanclosure.setLoan(loan);
            }
        }
        RequestContext.getCurrentInstance().addCallbackParam("success", found); 
    }
    
    public void search() {}
        
    private void initForm() {
        this.editMode = true;
        this.showLookup=false;
    }
        
    public final void newData() {  
        initForm();
        if (this.loanclosure!=null) loanclosure = null;
        this.loanclosure = new Loanclosure();
        insertionMode = true;
    }
    
    public void openData() {
        initForm();
        insertionMode = false;
    }
        
    public void save() {
        RequestContext context = RequestContext.getCurrentInstance();
        LoanclosureModel helper = new LoanclosureModel();
        boolean success;  
        
       // loanclosure.getLoan().setClosureDate(loanclosure.getClosureDate());
        success = helper.saveData(loanclosure);
                
        //if (!success && insertionMode) loanclosure.setLoanId(null);
        //else insertionMode = false;
        
        this.editMode = !success;
        Messager.showSaveUpdateStatus(success);     
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
                  
    }

    public String getDefaultButton() {
        return defaultButton;
    }

    public void setDefaultButton(String defaultButton) {
        this.defaultButton = defaultButton;
    }

    public boolean isShowLookup() {
        return showLookup;
    }

    public void setShowLookup(boolean showLookup) {
        this.showLookup = showLookup;
    }
    
    public void handleCloseLookup(CloseEvent event) {
        this.showLookup=false;
    }
    
     private void ambilUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean logBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        this.users = logBean.getSelectedUser();
    }
}
